package com.hbt.en_rem_hou.service;

import com.hbt.en_rem_hou.dao.FeatureDao;
import com.hbt.en_rem_hou.dao.WordDao;
import com.hbt.en_rem_hou.dao.WordFeaDao;
import com.hbt.en_rem_hou.entitys.Feature;
import com.hbt.en_rem_hou.entitys.Word;
import com.hbt.en_rem_hou.entitys.WordFea;
import com.hbt.en_rem_hou.entitys.WordsCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class WordServiceImp implements WordService{
    @Autowired
    WordDao wordDao;
    @Autowired
    WordFeaDao wordFeaDao;
    @Autowired
    FeatureDao featureDao;


    @Override
    public int deleteWordByWordId(int wordid) {
        return wordDao.deleteWordByWordId(wordid);
//        return 0;
    }

    @Override
    public int updateCmd(Word word) {
        return wordDao.updateCmdByCmdAndWordId(word);
//        return 0;
    }

    @Override
    public int updateImp(Word word) {
        return wordDao.updateImpByImdAndWordId(word);
//        return 0;
    }

    @Override
    public int changeFanyiByWordId(Word word) {
        return wordDao.updateTgtByWordId(word);
//        return 0;
    }

    @Override
    public boolean addWord(Word word) {
        //判断数据库是否存在这一个单词， 若存在则什么也不做
        if(hasThisWord(word)){
           return false;
        }else {
            //将word插入数据库， 并将feature插入数据库， 同时将获取的wordid和featureid插入w_f数据库
            //插入word
            wordDao.insertWord(word);
            System.out.println("***************插入word成功");
            System.out.println("word插入成功后为："+word);

            //插入feature
            for(Feature feature : word.getFeatures()){
                Feature fea = featureDao.getFeature(feature);
                if (fea==null){
                    featureDao.insertFeature(feature);
                    System.out.println("這是新的feature**********插入feature成功");
                    System.out.println("feature插入成功后为："+feature);
                }else{
                    System.out.println("feature是已經存在的："+fea);
                    word.getFeatures().get(word.getFeatures().indexOf(feature)).setId(fea.getId());
                }
            }
            //插入w_f
            for(Feature f: word.getFeatures()){

                WordFea wordFea = new WordFea();
                wordFea.setWordid(word.getId());
                wordFea.setFeatureid(f.getId());
                wordFeaDao.insertWordFea(wordFea);
                System.out.println("插入w_f成功");
            }
        }
        return true;
    }

    @Override
    public boolean hasThisWord(Word word) {
        Word w = wordDao.getWordByuserIdAndSrc(word);
        if(w != null){
            return true;
        }
        return false;
    }

    @Override
    public List<Word> getWordByCondition(WordsCondition wordsCondition) {
        return wordDao.getWordsByCondition(wordsCondition);
//        return null;
    }

    @Override
    public Word getWordByWordID(int wordid) {
        return wordDao.getWordByWordId(wordid);
//        return null;
    }
}
